using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNA_Game_Engine.Canvas;
using XNA_Game_Engine.Common;
using System;
using Microsoft.Xna.Framework.Media;
using XNA_Game_Engine.GUI;
using XNA_Game_Engine.Sounds;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using XNA_Game_Engine.FileManagment;


namespace MenesDroid
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Core_Engine : Microsoft.Xna.Framework.Game
    {
        //XNA content
        public GraphicsDeviceManager graphics;
        private SpriteBatch _spriteBatch;
        private Canvas _displayCanvas;
        private Texture2D cursor;
        

        public Core_Engine()
        {
            ActionsHandler.Game = this;
            graphics = new GraphicsDeviceManager(this)
                           {
                               PreferredBackBufferWidth = 1024,
                               PreferredBackBufferHeight = 768,
                               IsFullScreen = false,
                           };


            Content.RootDirectory = "Content";
            
            //SoundController.mute();
        }


        protected override void Initialize()
        {
            SaveGameManager.Load();

            this.IsMouseVisible = false; // shows mouse pointer            
            
            GameProperties.ScreenWidth = Window.ClientBounds.Width;
            GameProperties.ScreenHeight = Window.ClientBounds.Height;

            // XBLA support
           //Components.Add(new GamerServicesComponent(this));

            base.Initialize();
        }


        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            loadSounds();

            LoadMainMenuCanvas();

            cursor = Content.Load<Texture2D>("cursor");
        }


        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }


        protected override void Update(GameTime gameTime)
        {
            Input.Update(gameTime);
            // TODO: Add your update logic here
            float difftime = gameTime.ElapsedGameTime.Milliseconds;

            _displayCanvas.Update(gameTime);

            //botoes.Update();

            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.BurlyWood);

            _spriteBatch.Begin();
            _displayCanvas.Render(_spriteBatch);

            _spriteBatch.Draw(cursor, new Vector2( Mouse.GetState().X, Mouse.GetState().Y ), Color.White );
            _spriteBatch.End();  


            base.Draw(gameTime);
        }


        public void LoadLevelSelectionCanvas()
        {
            _displayCanvas = new LevelSelectionCanvas(this);
            _displayCanvas.LoadContent(Content);

        }

        public void LoadGameCanvas()
        {
            _displayCanvas = new GameCanvas(this, GetLevelString() );
            _displayCanvas.LoadContent(Content);
        }

        public void LoadMainMenuCanvas()
        {
            _displayCanvas = new MainMenuCanvas(this);
            _displayCanvas.LoadContent(Content);
            
        }

        public void GameOver()
        {
            this.Exit();
        }


        private String GetLevelString()
        {
            switch (ActionsHandler.level)
            {
                case 1: return "Content/Maps/level_01.map";
                case 2: return "Content/Maps/level_02.map";
                case 3: return "Content/Maps/level_03.map";
                case 4: return "Content/Maps/level_04.map";
                case 5: return "Content/Maps/level_05.map";
                case 6: return "Content/Maps/level_06.map";
                case 7: return "Content/Maps/level_07.map";
                case 8: return "Content/Maps/level_08.map";
                case 9: return "Content/Maps/level_09.map";
                case 10: return "Content/Maps/level_10.map";
                default: return null;
            }

        }

        private void loadSounds()
        {
            SoundController.menuMusic = Content.Load<Song>(@"SoundEffects\Menu");
            SoundController.fase1 = Content.Load<Song>(@"SoundEffects\Fase1");
            SoundController.lvlCompleted = Content.Load<Song>(@"SoundEffects\levelWin");
            SoundController.slot = Content.Load<SoundEffect>(@"SoundEffects\slot");
            SoundController.slotMiss = Content.Load<SoundEffect>(@"SoundEffects\slotMiss");
            SoundController.robotDeath = Content.Load<SoundEffect>(@"SoundEffects\death");
            SoundController.moveAhead = Content.Load<SoundEffect>(@"SoundEffects\moveAhead");
            SoundController.turn = Content.Load<SoundEffect>(@"SoundEffects\turns3");
            SoundController.jump = Content.Load<SoundEffect>(@"SoundEffects\jump");
            SoundController.enemyKill = Content.Load<SoundEffect>(@"SoundEffects\enemyKill");
        }

        

    }
}
